using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Dtos
{
    public partial class Task : Node<Task>
    {
        public Task()
        {
            this.Draggable = true;
            this.Resizable = true;
            this.Assignments = new HashSet<Assignment>();
            this.Predecessors = new HashSet<Dependency>();
            this.Successors = new HashSet<Dependency>();
            this.ChildrenRaw = new HashSet<Task>();
            this.Segments = new HashSet<TaskSegment>();
        }

        public override int Id { get; set; }
        public override Nullable<int> parentIdRaw { get; set; }
        public string Name { get; set; }
        public Nullable<System.DateTime> StartDate { get; set; }
        public Nullable<System.DateTime> EndDate { get; set; }
        public Nullable<decimal> Duration { get; set; }
        public string DurationUnit { get; set; }
        public Nullable<int> PercentDone { get; set; }
        public string SchedulingMode { get; set; }
        public Nullable<System.DateTime> BaselineStartDate { get; set; }
        public Nullable<System.DateTime> BaselineEndDate { get; set; }
        public Nullable<int> BaselinePercentDone { get; set; }
        public string Cls { get; set; }
        public Nullable<int> index { get; set; }
        public bool expanded { get; set; }
        public Nullable<decimal> Effort { get; set; }
        public string EffortUnit { get; set; }
        public string Note { get; set; }
        public string ConstraintType { get; set; }
        public Nullable<System.DateTime> ConstraintDate { get; set; }
        public bool ManuallyScheduled { get; set; }
        public bool Draggable { get; set; }
        public bool Resizable { get; set; }
        public bool Rollup { get; set; }
        public bool ShowInTimeline { get; set; }
        public string Color { get; set; }

        public virtual Task Parent { get; set; }
        public virtual ICollection<TaskSegment> Segments { get; set; }






        [JsonIgnore]
        public Nullable<int> CalendarIdRaw { get; set; }
        [JsonIgnore]
        public virtual ICollection<Task> ChildrenRaw { get; set; }
        [JsonIgnore]
        public virtual ICollection<Assignment> Assignments { get; set; }
        [JsonIgnore]
        public virtual Calendar Calendar { get; set; }
        [JsonIgnore]
        public virtual ICollection<Dependency> Predecessors { get; set; }
        [JsonIgnore]
        public virtual ICollection<Dependency> Successors { get; set; }









        [JsonIgnore]
        public string PhantomCalendarId { get; set; }

        public string CalendarId
        {
            set
            {
                PhantomCalendarId = value;
                if (value == null) return;

                try
                {
                    CalendarIdRaw = Convert.ToInt32(value);
                }
                catch (System.Exception)
                {
                    CalendarIdRaw = null;
                }
            }

            get
            {
                return CalendarIdRaw > 0 ? Convert.ToString(CalendarIdRaw) : PhantomCalendarId;
            }
        }

        public override bool leaf { get { return ChildrenRaw.Count == 0; } }

        public virtual ICollection<Task> children
        {
            get { if (!leaf) return ChildrenRaw; return null; }
            set { ChildrenRaw = value; }
        }

        public EF.Task ToEF()
        {
            return AutoMapper.Mapper.Map<EF.Task>(this);
        }
    }
     
}
